package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysTenant;
import com.ruoyi.system.service.ISysTenantService;

/**
 * 租户信息操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/tenant")
public class SysTenantController extends BaseController
{
    @Autowired
    private ISysTenantService tenantService;

    /**
     * 获取租户列表
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTenant tenant)
    {
        startPage();
        List<SysTenant> list = tenantService.selectTenantList(tenant);
        return getDataTable(list);
    }
    
    /**
     * 导出租户列表
     */
    @Log(title = "租户管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:tenant:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response, SysTenant tenant)
    {
        List<SysTenant> list = tenantService.selectTenantList(tenant);
        ExcelUtil<SysTenant> util = new ExcelUtil<SysTenant>(SysTenant.class);
        util.exportExcel(response, list, "租户数据");
    }

    /**
     * 根据租户编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:query')")
    @GetMapping(value = "/{tenantId}")
    public AjaxResult getInfo(@PathVariable Long tenantId)
    {
        return success(tenantService.selectTenantById(tenantId));
    }
    
    /**
     * 获取租户下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect()
    {
        List<SysTenant> tenants = tenantService.selectTenantAll();
        return AjaxResult.success(tenants);
    }

    /**
     * 新增租户
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:add')")
    @Log(title = "租户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysTenant tenant)
    {
        SysTenant existTenant = tenantService.selectTenantByCode(tenant.getTenantCode());
        if (existTenant != null)
        {
            return error("新增租户'" + tenant.getTenantName() + "'失败，租户编码已存在");
        }
        tenant.setCreateBy(getUsername());
        return toAjax(tenantService.insertTenant(tenant));
    }

    /**
     * 修改租户
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:edit')")
    @Log(title = "租户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysTenant tenant)
    {
        SysTenant existTenant = tenantService.selectTenantByCode(tenant.getTenantCode());
        if (existTenant != null && !existTenant.getTenantId().equals(tenant.getTenantId()))
        {
            return error("修改租户'" + tenant.getTenantName() + "'失败，租户编码已存在");
        }
        tenant.setUpdateBy(getUsername());
        return toAjax(tenantService.updateTenant(tenant));
    }

    /**
     * 获取租户选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<SysTenant> tenants = tenantService.selectTenantList(new SysTenant());
        return success(tenants);
    }
}